package com.example.poverty.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.poverty.entity.Cadre;
import com.example.poverty.service.CadreService;
import com.example.poverty.utils.Const;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * 前端驻村干部控制类
 */
@RestController
@RequestMapping("/cadre")
public class CadreController {

    @Autowired
    private CadreService cadreService;

    /**
     * 添加前端用户（驻村干部）
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Object addCadre(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String username = request.getParameter("username").trim();  //账号
        String password = request.getParameter("password").trim();  //密码
        String name = request.getParameter("name").trim();  //姓名
        String sex = request.getParameter("sex").trim();  //性别
        String phone = request.getParameter("phone").trim();  //电话
        String email = request.getParameter("email").trim();  //电子邮箱
        String location = request.getParameter("location").trim();  //驻村地址

        if (username == null || username.equals("")) {
            jsonObject.put(Const.CODE, 0);
            jsonObject.put(Const.MSG, "用户名不能为空");
            return jsonObject;
        }

        Cadre cadre1 = cadreService.getByName(name);
        if (cadre1 != null) {
            jsonObject.put(Const.CODE, 0);
            jsonObject.put(Const.MSG, "用户名已存在");
            return jsonObject;
        }

        if (password == null || password.equals("")) {
            jsonObject.put(Const.CODE, 0);
            jsonObject.put(Const.MSG, "密码不能为空");
            return jsonObject;
        }

        Cadre cadre = new Cadre();
        cadre.setUsername(username);
        cadre.setPassword(password);
        cadre.setName(name);
        cadre.setSex(new Byte(sex));
        cadre.setPhone(phone);
        cadre.setEmail(email);
        cadre.setLocation(location);
        boolean flag = cadreService.insert(cadre);
        if (flag) { //保存成功
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "添加成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "添加失败");
        return jsonObject;
    }

    /**
     * 驻村干部 修改
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Object updateCadre(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String id = request.getParameter("id").trim();  // 主键
        String username = request.getParameter("username").trim();  //账号
        String password = request.getParameter("password").trim();  //密码
        String name = request.getParameter("name").trim();  //姓名
        String sex = request.getParameter("sex").trim();  //性别
        String phone = request.getParameter("phone").trim();  //电话
        String email = request.getParameter("email").trim();  //电子邮箱
        String location = request.getParameter("location").trim();  //驻村地址

        Cadre cadre = new Cadre();
        cadre.setId(Integer.parseInt(id));
        cadre.setUsername(username);
        cadre.setPassword(password);
        cadre.setName(name);
        cadre.setSex(new Byte(sex));
        cadre.setPhone(phone);
        cadre.setEmail(email);
        cadre.setLocation(location);
        boolean flag = cadreService.update(cadre);
        if (flag) { //保存成功
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "修改成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "修改失败");

        return jsonObject;
    }

    /**
     * 删除 驻村干部信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET)
    public Object deleteCadre(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  //主键
        boolean flag = cadreService.delete(Integer.parseInt(id));
        return flag;
    }

    /**
     * 根据主键查询驻村干部信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/selectByPrimaryKey", method = RequestMethod.GET)
    public Object selectByPrimaryKey(HttpServletRequest request) {
        String id = request.getParameter("id").trim();  //主键
        return cadreService.selectByPrimaryKey(Integer.parseInt(id));
    }

    /**
     * 查询所有的驻村干部的信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/allCadre", method = RequestMethod.GET)
    public Object allCadre(HttpServletRequest request) {
        return cadreService.allCadre();
    }

    /**
     * 根据姓名查询驻村干部信息
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/getByName", method = RequestMethod.GET)
    public Object getByName(HttpServletRequest request) {
        String name = request.getParameter("name").trim();
        return cadreService.getByName(name);
    }

    /**
     * 前端 驻村干部 登录
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Object login(HttpServletRequest request) {
        JSONObject jsonObject = new JSONObject();
        String username = request.getParameter("username").trim();  //账号
        String password = request.getParameter("password").trim();  //密码
        if (username == null || username.equals("")) {
            jsonObject.put(Const.CODE, 0);
            jsonObject.put(Const.MSG, "用户名不能为空");
            return jsonObject;
        }
        if (password == null || password.equals("")) {
            jsonObject.put(Const.CODE, 0);
            jsonObject.put(Const.MSG, "密码不能为空");
            return jsonObject;
        }

        Cadre cadre = new Cadre();
        cadre.setUsername(username);
        cadre.setPassword(password);
        boolean flag = cadreService.verifyPassword(username, password);
        if (flag) {
            jsonObject.put(Const.CODE, 1);
            jsonObject.put(Const.MSG, "登陆成功");
            return jsonObject;
        }
        jsonObject.put(Const.CODE, 0);
        jsonObject.put(Const.MSG, "用户名和密码错误");
        return jsonObject;
    }

}
